import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class SuperMarket {

    private BigDecimal discont;

    //满减门槛
    private BigDecimal threshold;

    //满减值
    private BigDecimal  off;

    public SuperMarket(BigDecimal discount, BigDecimal threshold, BigDecimal off) {
        checkParameter(discount,threshold,off);
        this.discont = discount;
        this.threshold = threshold;
        this.off = off;
    }

    private void checkParameter(BigDecimal discount, BigDecimal threshold, BigDecimal off) {
        if(discount==null || discount.compareTo(BigDecimal.ZERO) < 0 || discount.compareTo(BigDecimal.ONE) > 0){
            throw new IllegalArgumentException("折扣率必须在0到1之间");
        }
        if(threshold==null || threshold.compareTo(BigDecimal.ZERO) < 0 ){
            throw new IllegalArgumentException("促销价必须大于等于0");
        }
        if(threshold==null || off.compareTo(BigDecimal.ZERO) < 0 ){
            throw new IllegalArgumentException("满减值必须大于等于0");
        }
    }

    public BigDecimal getTotalPrice(List<Fruit> fruits){
        BigDecimal sum=BigDecimal.ZERO;
        for (Fruit fruit:fruits){
            sum = sum.add(fruit.getTotalPrice());
        }
        System.out.println("最初的钱"+sum);

        BigDecimal discontPrice=sum.multiply(discont).setScale(2, RoundingMode.HALF_UP);;

        System.out.println("折扣后的钱"+discontPrice);

        if(discontPrice.compareTo(threshold)>=0){
            BigDecimal offPrice=discontPrice.subtract(off);
            System.out.println("满减后的钱"+offPrice);
            return offPrice;
        }
        return discontPrice;
    }
}
